package org.jeecg.modules.system.controller;

import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.Application;
import org.jeecg.modules.system.entity.CommonUsedApp;
import org.jeecg.modules.system.service.IApplicationService;
import org.jeecg.modules.system.service.ICommonUsedAppService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 用户常用表
 *
 * @Description: 用户常用表
 * @Version: V1.0
 */
@RestController
@RequestMapping("/commonUsedApp")
@Slf4j
@Api(tags = "用户常用应用")
public class CommonUsedAppController extends JeecgController<CommonUsedApp, ICommonUsedAppService> {
    @Resource
    private ICommonUsedAppService commonUsedAppService;
    @Resource
    private IApplicationService applicationService;

    /**
     * 分页列表查询
     *
     * @param commonUsedApp
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "用户常用表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<CommonUsedApp>> queryPageList(CommonUsedApp commonUsedApp,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      HttpServletRequest req) {
        QueryWrapper<CommonUsedApp> queryWrapper = QueryGenerator.initQueryWrapper(commonUsedApp, req.getParameterMap());
        Page<CommonUsedApp> page = new Page<CommonUsedApp>(pageNo, pageSize);
        IPage<CommonUsedApp> pageList = commonUsedAppService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 用户常用  应用列表
     *
     * @return
     */
    @GetMapping(value = "/queryList")
    @ApiOperation(value = "用户常用应用列表",notes = "用户常用应用列表")
    public Result<List<CommonUsedApp>> queryList() {
        List<CommonUsedApp> list = null;
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            LambdaQueryWrapper<CommonUsedApp> queryWrapper = new LambdaQueryWrapper<CommonUsedApp>()
                    .eq(CommonUsedApp::getUserId, user.getId());

            list = commonUsedAppService.list(queryWrapper);
            if (CollectionUtils.isNotEmpty(list)) {
                list.forEach(cua -> {
                    Application app = applicationService.getById(cua.getAppId());
                    cua.setApplicationIco(app.getApplicationIco());
                    cua.setRequestadd(app.getRequestadd());
                });
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return Result.OK(list);
    }

    /**
     * 添加
     *
     * @param
     * @return
     */
    @AutoLog(value = "用户常用表-添加")
    @PostMapping(value = "/add")
    @ApiOperation(value = "用户常用表-添加",notes = "用户常用表-添加")
    public Result<String> add(@RequestBody JSONArray jsonArray) {

        try {
            commonUsedAppService.addApp(jsonArray);
            return Result.OK("添加成功！");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 编辑
     *
     * @param commonUsedApp
     * @return
     */
    @AutoLog(value = "用户常用表-编辑")
    @RequiresPermissions("userComnApp:common_used_app:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody CommonUsedApp commonUsedApp) {
        commonUsedAppService.updateById(commonUsedApp);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户常用表-通过id删除")
    @RequiresPermissions("userComnApp:common_used_app:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        commonUsedAppService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "用户常用表-批量删除")
    @RequiresPermissions("userComnApp:common_used_app:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.commonUsedAppService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "用户常用表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<CommonUsedApp> queryById(@RequestParam(name = "id", required = true) String id) {
        CommonUsedApp commonUsedApp = commonUsedAppService.getById(id);
        if (commonUsedApp == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(commonUsedApp);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param commonUsedApp
     */
    @RequiresPermissions("userComnApp:common_used_app:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CommonUsedApp commonUsedApp) {
        return super.exportXls(request, commonUsedApp, CommonUsedApp.class, "用户常用表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("userComnApp:common_used_app:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CommonUsedApp.class);
    }

}
